package com.changgou.goods.controller;

import com.changgou.goods.pojo.Para;
import com.changgou.goods.service.ParaService;
import com.changgou.goods.service.SpecService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/para")
@CrossOrigin
public class ParaController {

    @Autowired
    private ParaService paraService;

    /****
     * 查找所有参数列表
     * @return
     */
    @GetMapping
    public Result<List<Para>> findAll() {
        List<Para> paras = paraService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", paras);
    }

    /****
     * 添加参数
     * @param para
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Para para) {
        paraService.add(para);
        return new Result<>(true, StatusCode.OK, "添加成功");
    }

    /****
     * 条件查询所有
     * @param para
     * @return
     */
    @PostMapping("/search")
    public Result<List<Para>> findList(@RequestBody(required = false) Para para) {
        List<Para> paras = paraService.findList(para);
        return new Result<>(true, StatusCode.OK, "查询成功", para);
    }

    /****
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable Integer page, @PathVariable Integer size) {
        PageInfo<Para> pageInfo = paraService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /****
     * 分页条件查询
     * @param page
     * @param size
     * @param para
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable Integer page, @PathVariable Integer size,
                                     @RequestBody(required = false) Para para) {
        PageInfo<Para> pageInfo = paraService.findPage(page, size, para);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /****
     * 根据Id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Para> findById(@PathVariable Integer id) {
        Para para = paraService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", para);
    }

    /****
     * 根据Id修改
     * @param id
     * @param para
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id, @RequestBody Para para) {
        para.setId(id);
        paraService.update(para);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    /****
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        paraService.delete(id);
        return new Result<>(true, StatusCode.OK, "删除成功");
    }

    /**
     * 根据分类ID查询参数列表
     * @param id
     * @return
     */
    @GetMapping(value = "/category/{id}")
    public Result<List<Para>> getByCategoryId(@PathVariable(value = "id")Integer id){
        //根据分类ID查询对应的参数信息
        List<Para> paras = paraService.findByCategoryId(id);
        return new  Result<List<Para>>(true,StatusCode.OK,"查询成功",paras);
    }
}
